package cn.jobstart.es.search;

import cn.jobstart.common.SpringUtils;
import cn.jobstart.common.StringUtil;
import cn.jobstart.common.YXDate;
import cn.jobstart.es.model.ResumeEs;
import cn.jobstart.props.FileConfig;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2023-09-06 22:04
 */

@Data
@ApiModel("简历es search")
public class ResumeEsSearch extends AbstractEsSearch<ResumeEs, ResumeEsSearch> {


    /**
     * 第三方求职者id
     */
    @ApiModelProperty("第三方求职者id")
    private String thirdAccountIdEq;
    @ApiModelProperty("学校标签")
    private String schoolLabelLike;
    @ApiModelProperty("按那些字段倒序")
    private List<String> orderByDesc;
    @ApiModelProperty("人才IDEq")
    private String personIdEq;
    @ApiModelProperty(value = "标题")
    private String titleLike;
    @ApiModelProperty(value = "关键词")
    private String keywordLike;
    private List ids;
    @ApiModelProperty(value = "工作经验年限")
    private String workExperienceLike;
    @ApiModelProperty(value = "学历 0:不限:unlimit,1:初中及以下:middle,2:高中/中技/中专:Senior,3:大专:junior,4:本科:undergraduate,5:硕士:Master,6:博士:doctor")
    private List educationFlagIn;
    @ApiModelProperty("学历文字")
    private String education;
    @ApiModelProperty("教育经历")
    private String educationalExperienceLike;
    @ApiModelProperty(value = "职位")
    private String jobNameLike;
    @ApiModelProperty(value = "行业")
    private String professionLike;
    @ApiModelProperty(value = "行业s")
    private List<String> professionList;
    @ApiModelProperty(value = "目前城市")
    private String presentCityLike;
    @ApiModelProperty(value = "期望城市")
    private String desiredCityLike;
    @ApiModelProperty(value = "期望城市s")
    private List<String> desiredCityList;
    @ApiModelProperty(value = "性别 0:不限:unlimit,1:男:male,2:女:female")
    private List sexIn;
    @ApiModelProperty(value = "最小年龄")
    private Integer ageGreater;
    @ApiModelProperty(value = "最大年龄")
    private Integer ageLess;
    @ApiModelProperty(value = "大于目前薪资")
    private Float currentSalaryGreater;
    @ApiModelProperty(value = "小于目前薪资")
    private Float currentSalaryLess;
    @ApiModelProperty(value = "大于期望薪资")
    private Float expectedSalaryGreater;
    @ApiModelProperty(value = "小于期望薪资")
    private Float expectedSalaryLess;
    @ApiModelProperty(value = "学校")
    private String schoolLike;
    @ApiModelProperty(value = "专业")
    private String specialityLike;
    @ApiModelProperty(value = "email")
    private String emailLike;
    @ApiModelProperty(value = "电话")
    private String mobileLike;
    @ApiModelProperty(value = "工作经验 0:不限:unlimit,1:1年以内:one_down,2:1-3年:one_three,3:3-5年:three_five,4: 5-10年:five_ten,5:10年以上:ten_up")
    private List workExperienceFlagIn;
    @ApiModelProperty(value = "简历ID")
    private String resumeIdEq;
    @ApiModelProperty(value = "订阅任务ID")
    private String talentSubscriptionTaskIdEq;
    @ApiModelProperty(value = "员工职位ID")
    private String adminJobIdEq;
    @ApiModelProperty(value = "身份证")
    private String idcardLike;
    private Date birthdayStart;
    private Date birthdayEnd;
    private Date resumeModifyTimeStart;
    private Date resumeModifyTimeEnd;
    private Date resumeCreateTimeStart;
    private Date resumeCreateTimeEnd;
    @ApiModelProperty(value = "职位s")
    private List jobNameList;
    private List<String> presentCityList;
    private String workCompanyLike;
    /**
     * 真实姓名
     */

    private String truenameLike;
    private String truenameEq;
    private String truenameLeftLike;
    private String truenameRightLike;
    private FileConfig fileConfig;
    @ApiModelProperty(value = "订阅任务ID空标识")
    private int talentSubscriptionTaskIdEmptyFlag = -1;

    public ResumeEsSearch() {
        fileConfig = (FileConfig) SpringUtils.getApplicationContext().getBean("fileConfig");
    }

    public static ResumeEsSearch getInstance() {

        ResumeEsSearch search = new ResumeEsSearch();
        search.trackScores();
        return search;
    }

    public ResumeEsSearch setThirdAccountIdEq(String thirdAccountIdEq) {
        this.thirdAccountIdEq = thirdAccountIdEq;
        eq(ResumeEs::getThirdAccountId, thirdAccountIdEq);
        return this;
    }

    public ResumeEsSearch setTalentSubscriptionTaskIdEmptyFlag(int talentSubscriptionTaskIdEmptyFlag) {
        this.talentSubscriptionTaskIdEmptyFlag = talentSubscriptionTaskIdEmptyFlag;
        if (talentSubscriptionTaskIdEmptyFlag == 1) {
            and(kw -> {

                not().isNotNull(ResumeEs::getTalentSubscriptionTaskId);


            });

        }
        return this;
    }

    public ResumeEsSearch setTruenameEq(String truenameEq) {
        this.truenameEq = truenameEq;
        eq(ResumeEs::getTruename, truenameEq);
        return this;
    }

    public ResumeEsSearch setSchoolLabelLike(String schoolLabelLike) {
        this.schoolLabelLike = schoolLabelLike;
        like(ResumeEs::getSchoolLabel, schoolLabelLike);
        return this;
    }

    public ResumeEsSearch setEducationalExperienceLike(String educationalExperienceLike) {
        this.educationalExperienceLike = educationalExperienceLike;
        match(ResumeEs::getEducationalExperience, educationalExperienceLike);

        return this;
    }

    public ResumeEsSearch setWorkCompanyLike(String workCompanyLike) {
        this.workCompanyLike = workCompanyLike;
        match(ResumeEs::getWorkExperience, workCompanyLike);
        return this;
    }

    public ResumeEsSearch setTruenameRightLike(String truenameRightLike) {
        this.truenameRightLike = truenameRightLike;
        likeRight(ResumeEs::getTruename, truenameRightLike);
        return this;
    }

    public ResumeEsSearch setTruenameLeftLike(String truenameLeftLike) {
        this.truenameLeftLike = truenameLeftLike;
        likeLeft(ResumeEs::getTruename, truenameLeftLike);
        return this;
    }

    public ResumeEsSearch setTruenameLike(String truenameLike) {
        this.truenameLike = truenameLike;
        like(ResumeEs::getTruename, truenameLike);

        return this;
    }

    public ResumeEsSearch setProfessionList(List<String> professionList) {
        this.professionList = professionList;
        if (StringUtil.isNotObjEmpty(professionList)) {

            and(kw -> {


                professionList.stream().flatMap(item -> Arrays.stream(item.split("/"))).forEach(item -> {

                    or().like(ResumeEs::getParentCategory, item);

                });


            });


        }
        return this;
    }

    public ResumeEsSearch setDesiredCityList(List<String> desiredCityList) {
        this.desiredCityList = desiredCityList;


        //  in(ResumeEs::getExceptCity, desiredCityList);


        if (StringUtil.isNotObjEmpty(desiredCityList)) {
            List<String> cityList = fileConfig.getCityListByProvinceList(desiredCityList);
            and(kw -> {
                cityList.stream().distinct().forEach(item -> {
                    if (StringUtil.isNullOrEmpty(item)) {
                        return;
                    }
                    String lastOneStr = item.substring(item.length() - 1);
                    if ("省".equalsIgnoreCase(lastOneStr) || "市".equalsIgnoreCase(lastOneStr) || "区".equalsIgnoreCase(lastOneStr)) {
                        or().like(ResumeEs::getExceptCity, item.substring(0, item.length() - 1));
                    } else {
                        or().like(ResumeEs::getExceptCity, item);
                    }


                });


            });

        }


        return this;
    }

    public ResumeEsSearch setResumeModifyTimeStart(Date resumeModifyTimeStart) {
        this.resumeModifyTimeStart = resumeModifyTimeStart;
        if (StringUtil.isNotObjEmpty(resumeModifyTimeStart)) {
            ge(ResumeEs::getResumeModifyTime, resumeModifyTimeStart.getTime());

        }
        return this;
    }

    public ResumeEsSearch setResumeModifyTimeEnd(Date resumeModifyTimeEnd) {
        this.resumeModifyTimeEnd = resumeModifyTimeEnd;
        if (StringUtil.isNotObjEmpty(resumeModifyTimeEnd)) {
            le(ResumeEs::getResumeModifyTime, resumeModifyTimeEnd.getTime());

        }
        return this;
    }

    public ResumeEsSearch setResumeCreateTimeStart(Date resumeCreateTimeStart) {
        this.resumeCreateTimeStart = resumeCreateTimeStart;
        if (StringUtil.isNotObjEmpty(resumeCreateTimeStart)) {
            ge(ResumeEs::getResumeModifyTime, resumeCreateTimeStart.getTime());

        }

        return this;
    }

    public ResumeEsSearch setResumeCreateTimeEnd(Date resumeCreateTimeEnd) {
        this.resumeCreateTimeEnd = resumeCreateTimeEnd;
        if (StringUtil.isNotObjEmpty(resumeCreateTimeEnd)) {
            le(ResumeEs::getResumeModifyTime, resumeCreateTimeEnd.getTime());

        }

        return this;
    }

    public ResumeEsSearch setBirthdayStart(Date birthdayStart) {
        this.birthdayStart = birthdayStart;
        if (StringUtil.isNotObjEmpty(birthdayStart)) {
            ge(ResumeEs::getBirthday, birthdayStart.getTime());
        }
        return this;
    }

    public ResumeEsSearch setBirthdayEnd(Date birthdayEnd) {
        this.birthdayEnd = birthdayEnd;
        if (StringUtil.isNotObjEmpty(birthdayEnd)) {
            le(ResumeEs::getBirthday, birthdayEnd.getTime());
        }

        return this;
    }

    public ResumeEsSearch setPersonIdEq(String personIdEq) {
        this.personIdEq = personIdEq;
        eq(ResumeEs::getPersonId, personIdEq);
        return this;
    }

    public ResumeEsSearch setIdcardLike(String idcardLike) {
        this.idcardLike = idcardLike;
        like(ResumeEs::getIdcard, idcardLike);
        return this;
    }

    public void setIds(List ids) {
        this.ids = ids;
        in(ResumeEs::getId, ids);
    }

    public void setEducation(String education) {
        this.education = education;
        like(ResumeEs::getEducation, education);
    }

    public ResumeEsSearch setTalentSubscriptionTaskIdEq(String talentSubscriptionTaskIdEq) {
        this.talentSubscriptionTaskIdEq = talentSubscriptionTaskIdEq;
        eq(ResumeEs::getTalentSubscriptionTaskId, talentSubscriptionTaskIdEq);
        return this;
    }

    public ResumeEsSearch setResumeIdEq(String resumeIdEq) {
        this.resumeIdEq = resumeIdEq;
        eq(ResumeEs::getId, resumeIdEq);
        return this;
    }

    public ResumeEsSearch setEmailLike(String emailLike) {
        this.emailLike = emailLike;
        like(ResumeEs::getEmail, emailLike);
        return this;
    }

    public ResumeEsSearch setMobileLike(String mobileLike) {
        this.mobileLike = mobileLike;
        like(ResumeEs::getMobile, mobileLike);
        return this;
    }

    public ResumeEsSearch setTitleLike(String titleLike) {
        this.titleLike = titleLike;
        like(ResumeEs::getTitle, titleLike);
        return this;
    }

    public ResumeEsSearch setKeywordLike(String keywordLike) {
        this.keywordLike = keywordLike;
        if (StringUtil.isNullOrEmpty(keywordLike)) {
            return this;
        }

        String[] keywordSplit = StringUtil.trimAll(keywordLike).split(" ");


        Arrays.stream(keywordSplit).forEach(item->{

            and(kw->{

                or().like(ResumeEs::getSummary, item)
                        .or().matchPhrase(ResumeEs::getOtherInfo, item)
                        .or().matchPhrase(ResumeEs::getAssociationActivity, item)
                        .or().matchPhrase(ResumeEs::getCapabilityLabel, item)
                        .or().matchPhrase(ResumeEs::getCertificate, item)
                        .or().matchPhrase(ResumeEs::getEducationalExperience, item)
                        .or().matchPhrase(ResumeEs::getEmploymentIntention, item)
                        .or().matchPhrase(ResumeEs::getInternship, item)
                        .or().matchPhrase(ResumeEs::getProfessionalSkills, item)
                        .or().matchPhrase(ResumeEs::getProjectExperience, item)
                        .or().matchPhrase(ResumeEs::getTrainingExperience, item)
                        .or().matchPhrase(ResumeEs::getWorkExperience, item)
                        .or().like(ResumeEs::getTruename, item)
                        .or().like(ResumeEs::getMobile, item);

            });


        });



       /* and(kw -> {


            Arrays.stream(keywordSplit).forEach(item -> {


                or().like(ResumeEs::getSummary, item)
                        .or().match(ResumeEs::getOtherInfo, item)
                        .or().match(ResumeEs::getAssociationActivity, item)
                        .or().match(ResumeEs::getCapabilityLabel, item)
                        .or().match(ResumeEs::getCertificate, item)
                        .or().match(ResumeEs::getEducationalExperience, item)
                        .or().match(ResumeEs::getEmploymentIntention, item)
                        .or().match(ResumeEs::getInternship, item)
                        .or().match(ResumeEs::getProfessionalSkills, item)
                        .or().match(ResumeEs::getProjectExperience, item)
                        .or().match(ResumeEs::getTrainingExperience, item)
                        .or().match(ResumeEs::getWorkExperience, item)
                        .or().like(ResumeEs::getTruename, item)
                        .or().like(ResumeEs::getMobile, item);


            });


        });*/

       /* Arrays.stream(keywordSplit).forEach(item -> {

            and(
                    kw -> kw.like(ResumeEs::getSummary, item)
                            .or().match(ResumeEs::getOtherInfo, item)
                            .or().match(ResumeEs::getAssociationActivity, item)
                            .or().match(ResumeEs::getCapabilityLabel, item)
                            .or().match(ResumeEs::getCertificate, item)
                            .or().match(ResumeEs::getEducationalExperience, item)
                            .or().match(ResumeEs::getEmploymentIntention, item)
                            .or().match(ResumeEs::getInternship, item)
                            .or().match(ResumeEs::getProfessionalSkills, item)
                            .or().match(ResumeEs::getProjectExperience, item)
                            .or().match(ResumeEs::getTrainingExperience, item)
                            .or().match(ResumeEs::getWorkExperience, item)
                            .or().like(ResumeEs::getTruename, item)
                            .or().like(ResumeEs::getMobile, item)


            );


        });*/


        return this;
    }

    public ResumeEsSearch setWorkExperienceFlagIn(List workExperienceFlagIn) {
        this.workExperienceFlagIn = workExperienceFlagIn;
        if (StringUtil.isNotObjEmpty(workExperienceFlagIn)) {
            if (workExperienceFlagIn.stream().anyMatch(item -> "0".equalsIgnoreCase(StringUtil.toString(item)))) {

                return this;
                // workExperienceFlagIn.add(null);
            }

            in(ResumeEs::getWorkExperienceFlag, workExperienceFlagIn);

        }

        return this;
    }

    public ResumeEsSearch setWorkExperienceLike(String workExperienceLike) {
        this.workExperienceLike = workExperienceLike;
        like(ResumeEs::getJobYear, workExperienceLike);

        return this;
    }


    public ResumeEsSearch setEducationFlagIn(List educationFlagIn) {
        this.educationFlagIn = educationFlagIn;
        if (StringUtil.isNotObjEmpty(educationFlagIn)) {
            if (educationFlagIn.stream().anyMatch(item -> "0".equalsIgnoreCase(StringUtil.toString(item)))) {

                return this;
            }
            in(ResumeEs::getEducationFlag, educationFlagIn);


        }

        return this;
    }

    public ResumeEsSearch setJobNameLike(String jobNameLike) {
        this.jobNameLike = jobNameLike;
        match(ResumeEs::getWorkExperience, jobNameLike);
        return this;
    }

    public ResumeEsSearch setJobNameList(List jobNameList) {
        this.jobNameList = jobNameList;

        if (StringUtil.isNotObjEmpty(jobNameList)) {


            and(

                    kw -> {
                        //  kw.match(ResumeEs::getWorkExperience, jobNameList.get(0));
                        jobNameList.stream().forEach(item -> {
                            or().match(ResumeEs::getWorkExperience, item);
                        });

                    }

            );


        }


        return this;
    }

    public ResumeEsSearch setProfessionLike(String professionLike) {
        this.professionLike = professionLike;
        like(ResumeEs::getParentCategory, professionLike);
        return this;
    }

    public ResumeEsSearch setPresentCityList(List<String> presentCityList) {
        this.presentCityList = presentCityList;

        if (StringUtil.isNotObjEmpty(presentCityList)) {
            List<String> cityList = fileConfig.getCityListByProvinceList(presentCityList);
            and(kw -> {
                cityList.stream().distinct().forEach(item -> {
                    if (StringUtil.isNullOrEmpty(item)) {
                        return;
                    }
                    String lastOneStr = item.substring(item.length() - 1);
                    if ("省".equalsIgnoreCase(lastOneStr) || "市".equalsIgnoreCase(lastOneStr) || "区".equalsIgnoreCase(lastOneStr)) {
                        or().like(ResumeEs::getLivePlace, item.substring(0, item.length() - 1));
                    } else {
                        or().like(ResumeEs::getLivePlace, item);
                    }


                });


            });

        }

        // in(ResumeEs::getLivePlace,presentCityList);
        return this;
    }

    public ResumeEsSearch setPresentCityLike(String presentCityLike) {
        this.presentCityLike = presentCityLike;
        like(ResumeEs::getLivePlace, presentCityLike);

        return this;
    }

    public ResumeEsSearch setDesiredCityLike(String desiredCityLike) {
        this.desiredCityLike = desiredCityLike;
        like(ResumeEs::getExceptCity, desiredCityLike);
        return this;
    }

    public ResumeEsSearch setSexIn(List sexIn) {
        this.sexIn = sexIn;
        in(ResumeEs::getSexFlag, sexIn);
        return this;
    }


    public ResumeEsSearch setSchoolLike(String schoolLike) {
        this.schoolLike = schoolLike;

        like(ResumeEs::getSchool, schoolLike);


        return this;
    }

    @Override
    public ResumeEsSearch getThis() {
        return this;
    }

    public ResumeEsSearch setSpecialityLike(String specialityLike) {
        this.specialityLike = specialityLike;
        like(ResumeEs::getSpeciality, specialityLike);
        return this;
    }


    public ResumeEsSearch setAgeGreater(Integer ageGreater) {
        this.ageGreater = ageGreater;

        if (StringUtil.isNotObjEmpty(ageGreater)) {
            Date startTime = YXDate.addYear(new Date(), -ageGreater.intValue());
            le(ResumeEs::getBirthday, startTime.getTime());
        }


        //   ge(ResumeEs::getAge, ageGreater);
        return this;
    }


    public ResumeEsSearch setAgeLess(Integer ageLess) {
        this.ageLess = ageLess;
        if (StringUtil.isNotObjEmpty(ageLess)) {
            Date startTime = YXDate.addYear(new Date(), -ageLess.intValue());
            ge(ResumeEs::getBirthday, startTime.getTime());
        }
        //  le(ResumeEs::getAge, ageLess);
        return this;
    }

    public ResumeEsSearch setCurrentSalaryGreater(Float currentSalaryGreater) {
        this.currentSalaryGreater = currentSalaryGreater;
        ge(ResumeEs::getCurrentSalary, currentSalaryGreater);
        return this;
    }

    public ResumeEsSearch setCurrentSalaryLess(Float currentSalaryLess) {
        this.currentSalaryLess = currentSalaryLess;
        le(ResumeEs::getCurrentSalary, currentSalaryLess);
        return this;
    }

    public ResumeEsSearch setExpectedSalaryGreater(Float expectedSalaryGreater) {
        this.expectedSalaryGreater = expectedSalaryGreater;
        ge(ResumeEs::getExpectedSalary, expectedSalaryGreater);
        return this;
    }

    public ResumeEsSearch setExpectedSalaryLess(Float expectedSalaryLess) {
        this.expectedSalaryLess = expectedSalaryLess;
        le(ResumeEs::getExpectedSalary, expectedSalaryLess);
        return this;
    }
}
